#include "widget.h"
#include "ui_widget.h"
#include "packag.h"
#include <QDebug>
Widget::Widget(int width,int height,QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    //登录连接服务器界面
    ui->setupUi(this);
    this->width=width;//获取手机的真实大小
    this->height=height;
    resize(this->width,this->height);
    backlb=new QLabel;
    backlb->setStyleSheet("border-image:url(:/back.jpg)");
    backlb->setFixedSize(this->width,this->height);
    QLabel *title1=new QLabel("月入百万");
    QLabel *jiao=new QLabel("连接即代表阅读并同意服务条款");
    title1->setAlignment(Qt::AlignHCenter);
    jiao->setAlignment(Qt::AlignHCenter);
    iple=new QLineEdit;
    iple->setPlaceholderText("ip");
    portle=new QLineEdit;
    portle->setPlaceholderText("port");
    lianjie=new QPushButton("连接");
    backlb->setParent(this);
    title1->setParent(this);
    iple->setParent(this);
    portle->setParent(this);
    lianjie->setParent(this);
    jiao->setParent(this);
    //iple->setText("112.74.125.150");
    //portle->setText("6060");

    //设置控件格式
    title1->setStyleSheet("font:25 36pt '华文彩云';"
                          "color: rgb(255,255,255);"
                          "padding-left:20px;" );
    jiao->setStyleSheet("color: rgb(255,255,255);"
                        "padding-left:20px;");
    iple->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                        "color: rgb(31,31,31);"		//字体颜色
                        "padding-left:20px;"       //内边距-字体缩进
                        "background-color: rgb(255, 255, 255);" //背景颜色
                        "border:2px solid rgb(20,196,188);border-radius:15px;");//边框粗细-颜色-圆角设置
    portle->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                          "color: rgb(31,31,31);"		//字体颜色
                          "padding-left:20px;"       //内边距-字体缩进
                          "background-color: rgb(255, 255, 255);" //背景颜色
                          "border:2px solid rgb(20,196,188);border-radius:15px;");//边框粗细-颜色-圆角设置
    lianjie->setStyleSheet("QPushButton{font: 25 14pt '微软雅黑 Light';color: rgb(255,255,255);background-color: rgb(33,168,224);"
                           "border: none;border-radius:15px;}"
                           "QPushButton:hover{background-color: rgb(33,168,224);}"//hover
                           "QPushButton:pressed{background-color: rgb(17,171,164);}");//pressed

    //设置控件位置
    title1->setGeometry(this->width/8,(this->height/6),(this->width/8)*6,200);
    iple->setGeometry(this->width/8,(this->height/3),(this->width/8)*6,70);
    portle->setGeometry(this->width/8,this->height/3+70+50,(this->width/8)*6,70);
    lianjie->setGeometry(this->width/8,this->height/3+70+70+150,(this->width/8)*6,70);
    jiao->setGeometry(this->width/8,(this->height/10)*9,(this->width/8)*6,70);

    //小车控制界面设计
    widget1=new QWidget;
    camlb=new QLabel;//定义需要的控件
    camlb->setScaledContents(true);
    camlb->setFixedSize(this->width,this->height);
    buzzpb=new QPushButton();
    buzzpb->setStyleSheet("border-image:url(:/buzz.png)");
    buzzpb->setFlat(true);
    gopb=new QPushButton();
    gopb->setStyleSheet("border-image:url(:/gopb.png)");
    gopb->setFlat(true);
    dial=new QDial;
    dial->setRange(0,360);
    dial->setGeometry(100,100,300,300);
    dial->setStyleSheet("background:transparent");
    camlb->setParent(widget1);
    buzzpb->setParent(widget1);
    dial->setParent(widget1);
    gopb->setParent(widget1);
    car_data=new activate_car;
    buzz_data=new trumpet;

    //设置控件位置
    gopb->setGeometry(100,this->height-300,200,200);
    buzzpb->setGeometry(400,this->height-300,200,200);

    //转到槽函数
    connect(lianjie,SIGNAL(clicked(bool)),this,SLOT(lianjietcp()));//连接小车
    connect(dial,SIGNAL(valueChanged(int)),this,SLOT(changeway(int)));//小车方向
    connect(gopb,SIGNAL(pressed()),this,SLOT(gocar()));//小车启动
    connect(gopb,SIGNAL(released()),this,SLOT(stopcar()));//小车停止
    connect(buzzpb,SIGNAL(pressed()),this,SLOT(buzz_on()));//喇叭启动
    connect(buzzpb,SIGNAL(released()),this,SLOT(buzz_off()));//喇叭停止
}
Widget::~Widget()
{
    delete ui;
}
void Widget::lianjietcp()//请求连接服务器
{
    client_connect(sock,iple->text(),portle->text().toInt());
    if(sock.state() == QAbstractSocket::UnconnectedState)
    {
        qDebug() << "连接服务器失败!请查看网络";
        return;
    }
    else
    {
        qDebug() << "连接成功!!!!!";
        first_signal(sock);//发送app上线信号
        connect(&sock,SIGNAL(readyRead()),this,SLOT(readcamdata()));
    }
}
QPixmap rotateImageWithTransform(const QPixmap &src, int angle)//图像旋转
{
    QMatrix matri;
    //迁移到中心
    matri.translate(src.width()/2.0,src.height()/2.0);
    //中心旋转
    matri.rotate(angle);
    //回退中心
    matri.translate(-src.width()/2.0,-src.height()/2.0);
    //执行坐标映射变化
    //旋转后图像大小变化了 需要提前进行裁剪 如果在旋转后裁剪
    //则需要计算使用三角函数计算
    //中心偏移
    int cubeWidth = qMin(src.width(),src.height());
    QRect cubeRect(0,0,cubeWidth,cubeWidth);
    cubeRect.moveCenter(src.rect().center());
    auto retImg = src.copy(cubeRect);
    retImg = retImg.transformed(matri,Qt::SmoothTransformation);
    return retImg;
}
void Widget::readcamdata()//接收数据
{
    struct head data_head;
    client_recv(sock,read_cmd,data_head);
    if(data_head.cmd_type==CAR_ACK)
    {
        this->hide();
        widget1->show();
    }
    else if(data_head.cmd_type==VIDEO)
    {
        char *buf = nullptr ;
        unpackCmdData(buf,read_cmd);
        QPixmap pix;
        pix.loadFromData((uchar *)buf,data_head.size,"JPEG");
        pix=rotateImageWithTransform(pix,90);
        camlb->setPixmap(pix);
        delete []buf;
    }
    delete []read_cmd;
    return;
}
void Widget::changeway(int num)//发送小车转动信息
{
    qDebug()<<num;
    car_data->degrees=num;
    packCmdData(*car_data,car_cmd);
    head data_head;
    unPackCmdData(data_head,car_cmd);
    client_send(sock,car_cmd);
    delete []car_cmd;
}
void Widget::gocar()//发送小车启动信息
{
    qDebug()<<"go";
    car_data->F=1;
    packCmdData(*car_data,car_cmd);
    head data_head;
    unPackCmdData(data_head,car_cmd);
    client_send(sock,car_cmd);
    delete []car_cmd;
}
void Widget::stopcar()//发送小车停止信息
{
    qDebug()<<"stop";
    car_data->F=0;
    packCmdData(*car_data,car_cmd);
    head data_head;
    unPackCmdData(data_head,car_cmd);
    client_send(sock,car_cmd);
    delete []car_cmd;
}
void Widget::buzz_on()//发送蜂鸣器启动信息
{
    qDebug()<<"buzz_on";
    buzz_data->mode=1;
    packCmdData(*buzz_data,buzz_cmd);
    head data_head;
    unPackCmdData(data_head,buzz_cmd);
    client_send(sock,buzz_cmd);
    delete []buzz_cmd;
}
void Widget::buzz_off()//发送蜂鸣器关闭信息
{
    qDebug()<<"buzz_off";
    buzz_data->mode=0;
    packCmdData(*buzz_data,buzz_cmd);
    head data_head;
    unPackCmdData(data_head,buzz_cmd);
    client_send(sock,buzz_cmd);
    delete []buzz_cmd;
}
